<template>
  <div class="container">
    <img src="@/assets/Login.jpg" alt="描述" class="clickable-image" @click="handleClick" />
    <div class="text-container">
      <p class="password-text">Reset your password</p>
    </div>
    <div class="divider"></div>

    <input v-model="password" type="text" placeholder="请输入密码" class="password-input"
      :class="{ 'error-border': passwordError }" @focus="clearError('password')" />

    <input v-model="passwordagain" type="text" placeholder="请再次输入密码" class="passwordagain-input"
      :class="{ 'error-border': passwordagainError }" @focus="clearError('passwordagain')" />

    <div class="container-v2">
      <el-row class="message">
        <el-col :span="24">
          输入新的密码:
        </el-col>
      </el-row>
      <el-row class="message2">
        <el-col :span="24">
          再次输入密码:
        </el-col>
      </el-row>
    </div>

    <button @click="verify" class="verify-button">确认修改</button>
    <p v-if="this.password !== this.passwordagain" class="error-message">{{ errorMessage }}</p>
  </div>

</template>

<script>
import { useRouter } from 'vue-router';
import { md5 } from 'js-md5';
import { ElMessage, ElNotification } from 'element-plus';
export default {
  data() {
    return {
      password: '',
      passwordagain: '',
      passwordError: false,
      passwordagainError: false,
      errorMessage: '',
      router: useRouter()
    };
  },
  methods: {
    handleClick() {
      if (this.router != null) {
        this.router.push('/login');
      }
    },
    clearError(type) {
      if (type === 'password') {
        this.passwordError = false;
      } else if (type === 'passwordagain') {
        this.passwordagainError = false;
      }
    },
    verify() {
      this.passwordError = false;
      this.passwordagainError = false;
      this.errorMessage = '';
      // 检查密码输入框
      if (!this.password) {
        this.passwordError = true;
        this.errorMessage = '';
      }

      // 检查密码二次输入框
      if (!this.passwordagain) {
        this.passwordagainErrorError = true;
        this.errorMessage = '';
      }

      // 如果存在错误，停止执行
      if (this.passwordError || this.passwordagainError) {
        return;
      }

      if (this.password !== this.passwordagain) {
        this.passwordError = true;
        this.passwordagainError = true;
        this.errorMessage = '两次输入的密码不一致，请重新输入！';
        return;
      }
      var url = 'http://localhost:8080/api/user/forgetPassword?email'
      fetch(url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        },
        body: JSON.stringify({
          email: localStorage.getItem('email'),
          password: md5(this.password),
        })
      }).then(response => {
        if (!response.ok) {
          throw new Error(`HTTP error! Status: ${response.status}`)
        }
        return response.json()
      }).then(data => {
        if (data['code'] != 200) {
          ElNotification({
            title: 'Error',
            message: data['msg'],
            type: 'error',
          })
        }
        else if (data['code'] == 200) {
          ElMessage({
            message: '修改密码成功',
            type: 'success'
          })
          localStorage.removeItem('email')
          if (this.router != null) {
            this.router.push('/login')
          }
        }
      }).catch(error => console.error('Error: ', error));

    },
  },
}
</script>

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.clickable-image {
  position: absolute;
  left: 10%;
  top: 0%;
  cursor: pointer;
  width: 8%;
  height: auto;
}

.text-container {
  position: absolute;
  top: 0%;
  text-align: center;
}

.password-text {
  font-family: 'Raleway', sans-serif;
  font-size: 36px;
  font-weight: bold;
  color: black;
}

.divider {
  position: absolute;
  width: 56%;
  top: 15%;
  height: 2px;
  background-color: #bbbbbb;
}

.password-input {
  position: absolute;
  top: 22%;
  /* 设置垂直位置 */
  left: 36%;
  /* 设置水平位置 */
  border: 1px solid #cccccc;
  border-radius: 5px;
  padding: 10px;
  width: 27%;
}

.passwordagain-input {
  position: absolute;
  top: 34%;
  /* 设置垂直位置 */
  left: 36%;
  /* 设置水平位置 */
  border: 1px solid #cccccc;
  border-radius: 5px;
  padding: 10px;
  width: 27%;
}

.verify-button {
  position: absolute;
  top: 42%;
  /* 设置垂直位置 */
  left: 36%;
  /* 设置水平位置 */
  background-color: #49a7f0;
  border: none;
  border-radius: 5px;
  color: white;
  padding: 10px;
  font-size: 18px;
  cursor: pointer;
}

.error-message {
  position: absolute;
  top: 50%;
  /* 设置错误信息的垂直位置 */
  left: 50%;
  /* 设置错误信息的水平位置 */
  transform: translateX(-50%);
  color: red;
  font-size: 12px;
}

.error-border {
  border: 1px solid red;
}

.container-v2 {
  position: flex;
  width: 100%;
  height: 100vh;

}

.message {
  position: absolute;
  left: 36%;
  top: 18%;
  font-size: 16px;
  color: black;
}

.message2 {
  position: absolute;
  left: 36%;
  top: 30%;
  font-size: 16px;
  color: black;
}

.verify-button {
  position: absolute;
  top: 42%;
  /* 设置垂直位置 */
  left: 36%;
  /* 设置水平位置 */
  background-color: #49a7f0;
  border: none;
  border-radius: 5px;
  color: white;
  padding: 10px;
  font-size: 18px;
}
</style>